package com.javasm.mingming.common.utils;

import com.javasm.mingming.common.exception.JavasmException;
import com.javasm.mingming.common.exception.JavasmExceptionEnum;
import io.jsonwebtoken.*;
import io.jsonwebtoken.io.Decoder;
import io.jsonwebtoken.io.Decoders;
import io.jsonwebtoken.io.Encoders;
import io.jsonwebtoken.security.Keys;
import io.jsonwebtoken.security.SignatureException;

import javax.crypto.SecretKey;
import java.beans.Encoder;
import java.rmi.server.UID;
import java.security.Key;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author javasm
 * @version 0.1
 * @className TokenUtil
 * @descriptioin:
 * @date 2025/1/8 9:56
 * @since jdk11
 */
public class TokenUtil {

    //服务端密钥,混淆的字符串,一旦确定,就不能改变,如果密钥改变,所有的token会集体失效
    private static final String keystr = "M/FmrR2+A9oyKfJJ6y4HBTOZ+XDlCCP9OuqcEhmC8S0=";
    //服务端ID 加密使用
    private static final String UID = "javasm";
    //加密的字符串,生效多长时间
    private static final Integer expTime = 1000 * 60 * 60 * 24 * 15;
    private static final Integer expMailTime = 1000 * 60 * 30;

    public static final String Server_Token_Key = "javasm_token";

    //生产秘钥
    private static void  generateKey(){
        //指定算法
        SecretKey key = Keys.secretKeyFor(SignatureAlgorithm.HS256);
        //生产加密字符串
        String encode = Encoders.BASE64.encode(key.getEncoded());
        System.out.println(encode);
    }

    //加密,生成Token
    public static String generateToken(String uid){

        return base(uid,expTime);
    }
    //解密,还原Token
    public static String parse(String token){
        Key secretKey = Keys.hmacShaKeyFor(Decoders.BASE64.decode(keystr));

        try {
            Jws<Claims> jws = Jwts.parserBuilder().setSigningKey(secretKey).build().parseClaimsJws(token);

            Claims body = jws.getBody();
            return body.get(UID, String.class);
        } catch (Exception e) {
            throw new JavasmException(JavasmExceptionEnum.SessionExpired);
        }
    }

    public static void main(String[] args) {
        //generateKey();
        /*String token = TokenUtil.generateToken("1001");
        System.out.println(token);*/
        String str = "eyJhbGciOiJIUzI1NiJ9.eyJqYXZhc20iOiIxMDAxIiwiaWF0IjoxNzM2MzAyMzUxLCJleHAiOjE3Mzc1OTgzNTF9.4QCoBqIia_XADTqwu9j8KzZXhSaGcbiLDyr8-p2XWmg";
        String uid = TokenUtil.parse(str);
        System.out.println(uid);

    }

    public static String generateEmailToken(String mail) {
        return base(mail,expMailTime);
    }

    public static String base(String str,Integer exp){
        Map<String,Object> map = new HashMap<>();
        map.put(UID,str);
        //因为加密的字符串有有效期,获取当前的时间戳
        Date time = new Date();
        Date lastTime = new Date(time.getTime() + exp);
        //生成key
        Key secretKey = Keys.hmacShaKeyFor(Decoders.BASE64.decode(keystr));
        String token = Jwts.builder()
                .setClaims(map)//自定义签名
                .setIssuedAt(time)//标准签名
                .setExpiration(lastTime)//过期时间
                .signWith(secretKey)//加密的密钥
                .compact();
        return token;
    }
}
